package com.test.spring.fusion.service.impl;

import java.util.List;

import org.spring.boot.fusion.common.tools.JsonTool;
import org.spring.boot.fusion.mapper.PageTool;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.spring.fusion.domain.User;
import com.test.spring.fusion.mapper.FusionUserMapper;
import com.test.spring.fusion.service.FusionUserService;
import com.test.spring.fusion.vo.UserVo;

@Service
public class FusionUserServiceImpl extends ServiceImpl<FusionUserMapper, User> implements FusionUserService {
	
	@Override
	public List<UserVo> findUserListByNameAndId(UserVo userVoParams) {
		
		//对象相互转换
		User formatClass = userVoParams.formatClass(User.class);
		userVoParams = formatClass.formatClass(UserVo.class);
		
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		LambdaQueryWrapper<User> lambda = queryWrapper.lambda();
		//查询用户名相等的
		String name = userVoParams.getNickName();
		if(name!=null && name.trim().length()>0) {
			lambda.eq(User::getNickName, name);
		}
		//执行查询
		List<User> list = this.list(queryWrapper);
		//转换数据返回给前端
		List<UserVo> listToList = JsonTool.ListToList(list, UserVo.class);
		return listToList;
	}
	
	
	@Override
	public IPage<UserVo> findUserListPage(UserVo userVoParams, int pageIndex, int pageSize) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		LambdaQueryWrapper<User> lambda = queryWrapper.lambda();
		// 查询用户名相等的
		String name = userVoParams.getNickName();
		if (name != null && name.trim().length() > 0) {
			lambda.eq(User::getNickName, name);
		}
		// 执行查询
		IPage<User> page = this.page(new Page<>(pageIndex, pageSize), queryWrapper);
		return PageTool.toClass(page, UserVo.class);
	}

}
